package org.example.soft.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.example.soft.Entity.AdminDTO;
import org.example.soft.Entity.UserDTO;
import org.example.soft.Entity.UserTodoDTO;
import org.example.soft.common.Result;
import org.example.soft.mapper.ToDoEventDTOmapper;
import org.example.soft.mapper.UserDTOmapper;
import org.example.soft.service.AdminDTOservice;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
@Tag(name = "AdminController",description = "管理员")
@RestController
@RequestMapping(value = "/admin")
public class AdminDTOController {

    @Autowired
    private AdminDTOservice AdminDTOService;
    @Resource
    UserDTOmapper userDTOmapper;
    @Resource
    ToDoEventDTOmapper toDoEventDTOmapper;

    @PostMapping("/register")
    public Result<String> register(@RequestBody AdminDTO admin){
        AdminDTOService.Register(admin);
        return Result.success();
    }
    @PostMapping("/login")
    public Result<String> login(@RequestBody AdminDTO admin){
        String name=admin.getAdminName();
        String password =admin.getPassword();
        String result = AdminDTOService.Login(name,password);
        if(result.equals("Login success")){
            return Result.success(result);
        }
        else return Result.fail(result);
    }
    //查询所有用户
    @GetMapping("/")
    @Operation(summary = "获取用户列表")
    public Result<List<UserDTO>> getUserList(){
        List<UserDTO> userDTOS = AdminDTOService.SelectallUser();
        return Result.success(userDTOS);
    }

    //增加用户
    @PostMapping("/add")
    @Operation(summary = "增加用户",description = "根据User对象创建用户")
    public Result<UserDTO> postUser(@RequestBody UserDTO userDTO){

        Long Id = AdminDTOService.Register(userDTO);
        return Result.success("InsertId:"+Id);
    }


    //通过id查询用户
    @GetMapping("/{id}")
    @Operation(summary = "通过id查询用户",description = "根据url的id来获取用户详细信息")
    public UserDTO getUserById(@PathVariable Long id){
        return this.AdminDTOService.SelectById(id);

    }

    //通过id更新用户
    @PutMapping("/{id}")
    @Parameter(name = "id",description = "用户编号",example = "1")
    @Operation(summary = "更新用户详细信息",description = "根据url的id来指定更新对象，并根据传过来的user信息来更新用户详细信息")

    public Result<Object> putUserById(@PathVariable Long id, @RequestBody UserDTO userDTO){
        System.out.println(userDTO);
        this.AdminDTOService.UpdateById(userDTO);
        return Result.success("更新成功");
    }


    //通过id删除用户
    @DeleteMapping("/{id}")
    @Operation(summary = "根据id删除用户",description = "根据url的id来指定删除对象")
    public Result<Object> deleteUser(@PathVariable Long id) {
        AdminDTOService.DeletedById(id);
        return Result.success("更新成功");
    }
    @DeleteMapping("/todo/{id}")
    @Operation(summary = "根据id删除todo",description = "根据url的id来指定删除对象")
    public Result<Object> deletetodo(@PathVariable Long id) {
        toDoEventDTOmapper.deleteEventById(id);
        return Result.success("更新成功");
    }

    @GetMapping("/getAlltodo")
    @Operation(summary = "获取全部用户的信息和待办事项")
    public Result<List<UserTodoDTO>> findPage(){
        List<UserTodoDTO> userDTOPage = toDoEventDTOmapper.selectTodolist();
        return Result.success(userDTOPage);
    }

    @GetMapping("/todo/{id}")
    @Operation(summary = "根据id获取用户信息和待办事项")
    public Result<Page<UserDTO>> getToDoListById(@PathVariable Long id) {
        Page<UserDTO> userDTOPage = userDTOmapper.getToDoListById(new Page<>(), id);
        return Result.success(userDTOPage);
    }
}







